<?php
// conflict massage model
namespace Admin\Model;
use Think\Model;

class ConflictModel extends Model{
	const username = '服务人员姓名';
	const customername = '客户姓名';
	const reward = '最终应得业绩';
	const source_type ='资源类型';	
	const create_time ='记录创建时间';
	public function addMsg($Arr = array('customer_id'=>NULL,'user_id'=>NULL,'checkboxMsg'=>NULL,'radioMsg'=>NULL,'dropdownMsg'=>NULL,'textarea'=>NULL,'note'=>NULL,'payamount'=>NULL,'dropdownUser'=>NULL)){
        $RtnArr = array('rcode'=>0,'data'=>'','msg'=>'OK');
        $d = $Pay = array();
		if ($Arr['customer_id'] == NULL || $Arr['user_id'] == NULL || $Arr['checkboxMsg'] == NULL || $Arr['dropdownMsg'] == NULL) {
			$RtnArr['msg'] = '请把信息填写完整';
			return $RtnArr;			
		}
		else{
			// add conflict database
			$res = M('user')->field('sector.parent_company,user.sector_id,user.role_id')
							->join('LEFT JOIN sector ON user.sector_id = sector.sector_id')
							->where(array('user.user_id'=>$Arr['user_id']))
							->find();
			$data = array('customer_id'=>$Arr['customer_id'],'claim_user_id_id'=>$Arr['user_id'],'claim_user_role'=>$res['role_id'],'claim_user_subcompany'=>$res['parent_company'],'create_time'=>date('Y-m-d H:i:s'),'notify'=>$Arr['textarea'],'customer_belong'=>$Arr['radioMsg']);
			if ($Arr['note']) {
				$data['remark'] = $Arr['note'];
			}
			$total = 0;
			$payres = 0;
	        $ex_ArrPay = explode(',', $Arr['payamount']);
	        foreach ($ex_ArrPay  as &$value) {
	        	$total += $value;
	        }				

			switch ($Arr['dropdownMsg']) {
				case '1':
					$data['claim_mount'] = $total;
					$data['status'] =$Arr['dropdownMsg'];
					break;
				case '2':
					// $data['claim_mount'] = $total/(count($ex_ArrPay)+1);
					foreach($ex_ArrPay as &$value){
						$payres += $value/2;
					}
					$data['claim_mount'] = $payres;
					$data['status'] =$Arr['dropdownMsg'];
					break;
				case '3':
					$data['claim_mount'] = 0;
					$data['status'] =$Arr['dropdownMsg'];
					break;
				case '4':
					$data['claim_mount'] = 0;
					$data['status'] =$Arr['dropdownMsg'];
					break;	
			}

			$conflictId = $this->add($data);
			if ($conflictId) {
				$conflictMsg = $this->where(array('conflict_id'=>$conflictId))->find();
				if (!$conflictMsg) {
					$RtnArr['msg'] = '撞单记录添加失败';
					return $RtnArr;
				}
			}

			// add conflict_pay database;
			$d['conflict_id'] = $conflictId;
			$pay_amount = explode(',', $Arr['payamount']);
	        $ex_ArrCheck = explode(',', $Arr['checkboxMsg']);
			foreach ($ex_ArrCheck as $key => $value) {
					switch ($Arr['dropdownMsg']) {
						case '1':
							$d['pay_id'] = $value;
							$d['last_mount'] = 0;
							$wheres_claimant['mount'] = $total;
							$wheres_respondent['mount'] = 0;
							break;
						case '2':
							$d['pay_id'] = $value;
							$d['last_mount'] = $pay_amount[$key]/2;
							$wheres_claimant['mount'] = $pay_amount[$key]/2;
							$wheres_respondent['mount'] = $pay_amount[$key]/2;
							break;
						case '3':
							$d['pay_id'] = $value;
							$d['last_mount'] = $pay_amount[$key];
							$wheres_claimant['mount'] = 0;
							$wheres_respondent['mount'] = $pay_amount[$key];
							break;
						case '4':
							$d['pay_id'] = $value;							
							$d['last_mount'] = '-'.$pay_amount[$key];
							$wheres_claimant['mount'] = 0;
							$wheres_respondent['mount'] = '-'.$pay_amount[$key];
							break;	
					}
					// add sales_change_log database claimant;
					$wheres_claimant['relate_id'] = $value;
					$wheres_claimant['user_id'] = $Arr['user_id'];
					$wheres_claimant['customer_id'] = $Arr['customer_id'];
					$wheres_claimant['conflict_id'] = $conflictId;
					$wheres_claimant['create_time'] = date('Y-m-d H:i:s');
					$wheres_claimant['relate_type'] = 1;
					$customerMsg = M('customer')->field('nickname')->where(array('customer_id'=>$Arr['customer_id']))->find();
					$userMsg = M('user')->field('nickname')->where(array('user_id'=>$Arr['user_id']))->find();
					$wheres_claimant['description'] = self::username.':'.$userMsg['nickname'].' '.self::customername.':'.$customerMsg['nickname'].' '.self::reward.':'.$wheres_claimant['mount'].' '.self::source_type.':'.'撞单'.' '.self::create_time.':'.date('Y-m-d H:i:s');
					if ($Arr['note']) {
						$wheres_claimant['remark'] = $Arr['note'];
					}
					// add sales_change_log database respondent
					// $respondent = M('pay_record')->field('sales_person')->where(array('payrecord_id'=>$value))->find();
					$respondent = M('pay_record')->field('sales_person')->where(array('pay_id'=>$value,'record_status'=>1))->find();
					$wheres_respondent['user_id'] = $respondent['sales_person'];					
					$wheres_respondent['relate_id'] = $value;
					$wheres_respondent['customer_id'] = $Arr['customer_id'];
					$wheres_respondent['conflict_id'] = $conflictId;
					$wheres_respondent['create_time'] = date('Y-m-d H:i:s');
					$wheres_respondent['relate_type'] = 1;
					$unionMsgRes= M('contract')
												->field('contract.contract_id,user.nickname,customer.nickname as customername')
												->join('user on user.user_id = contract.sales_person')
												->join('customer on customer.customer_id = contract.customer_id')
												->where(array('contract.sales_person'=>$respondent['sales_person'],'contract.customer_id'=>$Arr['customer_id']))
												->find();

					//20170727 Wjw 修 ：根据流水id查询合同id
					$maps['payrecord_id'] = $Arr['checkboxMsg'];
					$contract = M('pay_record')->field('contract_id')->where($maps)->find();
					if(!$unionMsgRes && !$contract){
						//有流水没有合同小单默认值
						$wheres_respondent['contract_id'] = -1;
						$wheres_claimant['contract_id'] = -1;
						$wheres_respondent['description'] = '';
					}else{
						$wheres_respondent['contract_id'] = $contract['contract_id'];
						$wheres_claimant['contract_id'] = $contract['contract_id'];
						$wheres_respondent['description'] = self::username.':'.$unionMsgRes['nickname'].' '.self::customername.':'.$unionMsgRes['customername'].' '.self::reward.':'.$wheres_respondent['mount'].' '.self::source_type.':'.'撞单'.' '.self::create_time.':'.date('Y-m-d H:i:s');					
					}
					//Wjw 修改结束
					if ($Arr['note']) {
						$wheres_respondent['remark'] = $Arr['note'];
					}
			     	$conflictPay = M('conflict_pay')->add($d);	
					$saleschangelogclaimant = M('sales_change_log')->add($wheres_claimant);	
					$slaeschangelogrespondent = M('sales_change_log')->add($wheres_respondent);			
					if(!$conflictPay || !$saleschangelogclaimant || !$slaeschangelogrespondent){
						$RtnArr['msg'] = '撞单加失败';
						return $RtnArr;						
					}
			     } 

			// update customer database 
			if($Arr['dropdownUser']  && $Arr['dropdownMsg'] != 4){
				$customer_sales = M('user')
											->field('user.user_id,user.sector_id,sector.manager_id,sector.parent_company')
											->join('LEFT JOIN sector ON sector.sector_id = user.sector_id')
											->where(array('user_id'=>$Arr['dropdownUser']))
											->find();
				$CusArray = array('sales_person'=>$customer_sales['user_id'],'sales_sector'=>$customer_sales['sector_id'],'sales_manager'=>$customer_sales['manager_id'],'sales_sub_company'=>$customer_sales['parent_company']);
				$SaveCustomer = M('customer')->where(array('customer_id'=>$Arr['customer_id']))->save($CusArray);
				if(!$SaveCustomer){
					$RtnArr['rcode'] = 0;
					$RtnArr['msg'] = '用户归属权修改失败';
				}
				return $RtnArr; 
			}elseif($Arr['dropdownMsg'] == 4){
				$CusArray = array('sales_person'=> -1,'sales_sector'=> -1,'sales_manager'=> -1,'sales_sub_company'=>-1);
				$SaveCustomer = M('customer')->where(array('customer_id'=>$Arr['customer_id']))->save($CusArray);
				if(!$SaveCustomer){
					$RtnArr['rcode'] = 0;
					$RtnArr['msg'] = '用户归属权修改失败';
				}
				return $RtnArr;				
			}
		}
		return $RtnArr;
	}

	// delete conflict records
	// public function delconflictrecord($Arr = array('conflict_id'=>NULL)){
	// 		$res = array('code'=>0,'data'=>'','msg'=>'');
	// 		$conflict_del = $this ->where(array('conflict_id'=>$Arr['conflict_id']))->save(array('is_delete'=>1));
	// 		$conflict_pay_del = M('conflict_pay') ->where(array('conflict_id'=>$Arr['conflict_id']))->delete();
	// 		$sales_change_log = M('sales_change_log') ->where(array('conflict_id'=>$Arr['conflict_id']))->delete();
	// 		if ($conflict_del && $conflict_pay_del) {
	// 				$res['code'] = 1;
	// 				$res['msg'] = '信息删除成功';
 	// 		}else
 	// 				$res['msg'] = '信息删除失败';
 	// 		return $res;
	// }
}
?>